<template>
  <el-row class="warp">
    <el-col :span="12" class="warp-main">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules" class="demo-ruleForm">
        <el-form-item label="原密码" prop="oldPwd">
          <el-input v-model="form.oldPwd"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd">
          <el-input v-model="form.newPwd"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPwd">
          <el-input v-model="form.confirmPwd"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
  import {post} from '@/utils/http'
  export default{
    data(){
      let validateOld = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入原密码'));
        } else {
          callback();
        }
      };
      let validateNew = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          callback();
        }
      };
      let validateSure = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('确认密码不能为空'));
        } else if(value != this.form.newPwd) {
          callback(new Error('两次密码输入的不一致'));
        } else {
          callback();
        }
      };
      return {
        form: {
          oldPwd: '',
          newPwd: '',
          confirmPwd: ''
        },
        rules: {
          oldPwd: [
            {required: true, validator: validateOld, trigger: 'blur' }
          ],
          newPwd: [
            {required: true,  validator: validateNew, trigger: 'blur' }
          ],
          confirmPwd: [
            {required: true,  validator: validateSure, trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      onSubmit(form) {
        this.$refs[form].validate((valid) => {
          let _this = this;
          if (valid) {
            let s = {"oldPWD":this.form.oldPwd,"password":this.form.newPwd}
            post('/api/manager/v1/user/updatePassWord',s)
              .then((response) => {
                this.$message({
                  message: '密码修改成功',
                  type: 'success'
                });
                setTimeout(function(){
                  _this.$refs[form].resetFields();
                },500)


              })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>
